const App = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    qualifications:[],
    user_data:[],
    nowcheckd:'',
    receive_status:0,
    userInfo:[],
    bgWidth:'100%',
    bgHeight: '100%',
    zige:0,
    srcImagePath:'http://zqg.dlaliyun.cn/assets/store/img/dong.gif',
    defaultImagePath:'http://zqg.dlaliyun.cn/assets/store/img/dong.jpg',
    loadComplete:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getinfo();
    this.getuserinfo();
  },
  imgOnLoad:function(ev) {
    console.log(ev);
    let src = ev.currentTarget.dataset.src,
     width = ev.detail.width,
     height = ev.detail.height
     this.setData({
      loadComplete:true
    })
     
   },
  godetail(){
    wx.navigateTo({
      url: '../rice/log/index'
    });
  },
  getuserinfo(){
    let _this = this;
    App._get('user.index/detail', {}, function(result) {
      console.log(result);
      _this.setData({
        userInfo:result.data.userInfo
      })
    })
  },
  gobuy(){
    setTimeout(()=>
    {
      wx.navigateTo({
        url: '../category/list?category_id=10008'
      });
    }, 2000)
  },
  getinfo(){
    let _this = this;
    App._get('rice/riceInfo', {}, function(result) {

      console.log(result);
      let qualifications=result.data.qualifications;
      let setting=result.data.setting;
      let user_data=result.data.user_data;
      let zige=result.data.zige;
      _this.setData({
        qualifications:qualifications,
        setting:setting,
        user_data:user_data,
        zige:zige
      })

    });
  },
  dowatering(){
    let _this = this;
    App._get('rice/watering', {}, function(result) {
      wx.showToast({
        title: '浇水完成！',
        icon: 'success',
        duration: 2000
      })
      _this.getinfo();
      _this.getuserinfo();
    })
  },
  doexchange(){
    let _this = this;
    let rice=_this.data.userInfo.rice;
    if(rice>0){
      let txt='您现在有'+rice+'米，可以兑换'+rice+'积分,是否兑换？';
      wx.showModal({
        title: '积分兑换',
        content: txt,
        success: function (res) {
          if (res.confirm) {
            App._get('rice/exchange', {}, function(result) {
              wx.showToast({
                title: '兑换成功！',
                icon: 'success',
                duration: 2000
              })
              _this.getinfo();
              _this.getuserinfo();
            })

          }else{
            console.log('用户点击取消');
          }
        }
      })
    }else{
      wx.showToast({
        title: '您的米数不足，无法兑换积分！',
        icon: 'none',
        duration: 2000
      })
    }
    
   
  },
  dostealrice(){
    let _this = this;
    App._get('rice/takeRice', {}, function(result) {
      wx.showToast({
        title: '偷米成功！',
        icon: 'success',
        duration: 2000
      })
      _this.getinfo();
      _this.getuserinfo();
    })
  },
  dostealone(e){
    let _this = this;
    console.log(e);
    App._get('rice/takeOne', {user_id:e.currentTarget.dataset.id}, function(result) {
      wx.showToast({
        title: '偷米成功！',
        icon: 'success',
        duration: 2000
      })
      _this.getinfo();
      _this.getuserinfo();
    })
    
  },
  doshowinfo(){
    wx.showModal({
      title: '规则介绍',
      content: this.data.setting.describe,
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})